﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;
using DTC.Database;
using System.Collections;

namespace DTC.Popups
{
    /// <summary>
    /// Interaction logic for DrugPrescriptionPopup.xaml
    /// </summary>
    public partial class DrugPrescriptionPopup : Window
    {
        public DrugPrescriptionPopup()
        {
            InitializeComponent();
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            drugPrescription = new DTCDrugPrescription();
            drugPrescription.CourseDurationDays = DTC.Converters.IntDaysToStringTimeSpan.NoEnd;
            MainGrid.DataContext = drugPrescription;
            DoctorComboBox.ItemsSource = ReadDoctors();
            DrugComboBox.ItemsSource = ReadDrugs();
            drugPrescription.Status = true;
            isNew = true;

            //ICollectionView view = CollectionViewSource.GetDefaultView(DrugComboBox.ItemsSource);
            //view.GroupDescriptions.Add(new PropertyGroupDescription("DTCDrug.Name"));
        }

        bool isNew;
        private DTCDrugPrescription drugPrescription;
        public DTCDrugPrescription DrugPrescription
        {
            get
            {
                return drugPrescription;
            }
            set
            {
                drugPrescription = value;
                if (drugPrescription.DTCPatient != null)
                {
                    MainGrid.DataContext = drugPrescription;
                }
                DrugComboBox.SelectedItem = drugPrescription.DTCDrugUnit.DTCDrug;
                DrugUnitsComboBox.SelectedItem = drugPrescription.DTCDrugUnit;
                DrugDoseTB.Text = drugPrescription.FormatedDose;
                DrugFreqOfUseTB.Text = drugPrescription.FormatedFreqOfUse;
                isNew = false;
                SetDoctor();
            }
        }
        public DTCPatient Patient
        {
            get
            {
                return drugPrescription.DTCPatient;
            }
            set
            {

                drugPrescription.DTCPatient = value;
                SetDoctor();
            }
        }

        private List<string> DosePeriods;

        private List<DTCDoctor> ReadDoctors()
        {
            return App.DBI.GetAllDoctors();
        }
        private List<DTCDrug> ReadDrugs()
        {
            return App.DBI.GetAllDrugs();
        }
        private List<DTCDrugUnit>drugUnits;


        private void SetDoctor()
        {
            DTCDoctor doc = null;
            if (drugPrescription.DTCDoctor != null)
                doc = drugPrescription.DTCDoctor;
            else if (drugPrescription.DTCPatient.DTCDoctor != null)
                doc = drugPrescription.DTCPatient.DTCDoctor;
            else return;
            DoctorComboBox.SelectedItem = doc;
                
        }
        private void SetDrug()
        {

            try
            {
                DrugComboBox.SelectedItem = drugPrescription.DTCDrugUnit.DTCDrug;
            }
            catch
            {}
        }

        private void Window_Closing(object sender, CancelEventArgs e)
        {
            if (DialogResult == true)
            {
                if (DrugUnitsComboBox.SelectedIndex == -1)
                {
                    WarningPopup wp = new WarningPopup();
                    wp.Title = "Внимание!";
                    wp.Message = "Необходимо указать форму выпуска";
                    wp.Owner = this;
                    wp.ShowDialog();
                    e.Cancel = true;
                    return;
                }
                drugPrescription.DTCDrugUnit = (DTCDrugUnit)DrugUnitsComboBox.SelectedItem;
                if (DrugDoseTB.Text != drugPrescription.FormatedDose)
                {
                    drugPrescription.FormatedDose = DrugDoseTB.Text;
                }
                if (DrugFreqOfUseTB.Text != drugPrescription.FormatedFreqOfUse)
                {
                    drugPrescription.FormatedFreqOfUse = DrugFreqOfUseTB.Text;
                }
                if (isNew)
                {
                    List<DTCOrder> AffectedOrders = App.DBI.GetOrdersByTime(drugPrescription.CourseStartDate.Value, drugPrescription.CourseStartDate.Value.AddDays(drugPrescription.CourseDurationDays.Value));
                    foreach (DTCOrder o in AffectedOrders)
                    {
                        o.PatientOrderStatuse = App.DBI.GetStatusByID((int)DTCOrder.OrderStatuses.Changed);
                    }
                    App.DBI.Db.DTCDrugPrescriptions.InsertOnSubmit(drugPrescription);
                }
                App.DBI.Db.SubmitChanges();
            }
            else
            {
                App.DBI.ChangeCancel();
            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            PrescDateTB.Text = DateTime.Today.ToShortDateString();
            CourseStartDateTB.Text = DateTime.Today.ToShortDateString();
            new DTC.Resources.TextBoxBindingValidationSetter().PrepareBinding(MainGrid);
        }

        private void DrugComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            drugUnits = new List<DTCDrugUnit>();
            if (DrugComboBox.SelectedIndex != -1)
            {
                DTCDrug tmpDrug = (DTCDrug)DrugComboBox.SelectedItem;
                foreach (DTCDrugUnit du in tmpDrug.DTCDrugUnits)
                {
                    drugUnits.Add(du);
                }
            }
            DrugUnitsComboBox.ItemsSource = drugUnits;
        }
    }                   
}
